package com.slicktechnologies.client.views.customer;

import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.TextBox;
import com.simplesoftwares.client.library.FieldType;
import com.simplesoftwares.client.library.FlexForm.FormStyle;
import com.simplesoftwares.client.library.FormField;
import com.simplesoftwares.client.library.FormFieldBuilder;
import com.simplesoftwares.client.library.appskeleton.AppMemory;
import com.simplesoftwares.client.library.appskeleton.ScreeenState;
import com.simplesoftwares.client.library.appstructure.formscreen.FormScreen;
import com.simplesoftwares.client.library.composite.AddressComposite;
import com.simplesoftwares.client.library.mywidgets.EmailTextBox;
import com.simplesoftwares.client.library.mywidgets.ObjectListBox;
import com.simplesoftwares.client.library.mywidgets.PhoneNumberBox;
import com.slicktechnologies.client.utility.AppUtility;
import com.slicktechnologies.client.utility.Screen;
import com.slicktechnologies.shared.common.businessprocesslayer.Customer;
import com.slicktechnologies.shared.common.businessunitlayer.Branch;
import com.slicktechnologies.shared.common.personlayer.Employee;


public class CustomerForm extends FormScreen<Customer> {

	//Token to add the varialble declarations
	
	ObjectListBox<Branch> olbbBranch;
	EmailTextBox etbEmail;
	TextBox tbFirstName,tbMiddleName,tbLastName;
	IntegerBox tbCustomerId;
	PhoneNumberBox pnbCellNo1;
	ObjectListBox<Employee> olbeSalesPerson;
	AddressComposite serviceAdressComposite;
	
	
	
	

	public  CustomerForm() {
		super();
		createGui();
		this.tbCustomerId.setEnabled(false);
		
		

	}

	public CustomerForm  (String[] processlevel, FormField[][] fields,
			FormStyle formstyle) 
	{
		super(processlevel, fields, formstyle);
		createGui();
	}


	/**
	 * Method template to initialize the declared variables.
	 */
	private void initalizeWidget()
	{

		olbbBranch=new ObjectListBox<Branch>();
		AppUtility.makeBranchListBoxLive(olbbBranch);
		etbEmail=new EmailTextBox();
		tbCustomerId=new IntegerBox();
		tbFirstName=new TextBox();
		tbMiddleName=new TextBox();
		tbLastName=new TextBox();
		pnbCellNo1=new PhoneNumberBox();
		olbeSalesPerson=new ObjectListBox<Employee>();
		AppUtility.makeSalesPersonListBoxLive(olbeSalesPerson);
		serviceAdressComposite=new AddressComposite();
		serviceAdressComposite.setNonManadatory(true);
	}

	/**
	 * method template to create screen formfields
	 */
	@Override
	public void createScreen() {


		initalizeWidget();

		//Token to initialize the processlevel menus.
		this.processlevelBarNames=new String[]{"Create Service Card","New"};



		//////////////////////////// Form Field Initialization////////////////////////////////////////////////////
		//Token to initialize formfield
		FormFieldBuilder fbuilder;
		fbuilder = new FormFieldBuilder();
		FormField fgroupingCustomerInformation=fbuilder.setlabel("Customer Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("Customer Id",tbCustomerId);
		FormField ftbCustomerId= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		
		
		
		
		fbuilder = new FormFieldBuilder("* First Name",tbFirstName);
		FormField ftbFirstName= fbuilder.setMandatory(true).setMandatoryMsg("First Name is mandatory!").setRowSpan(0).setColSpan(0).build();
		
		fbuilder = new FormFieldBuilder("Middle Name",tbMiddleName);
		FormField ftbMiddleName= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		
		fbuilder = new FormFieldBuilder("* Last Name",tbLastName);
		FormField ftbLastName= fbuilder.setMandatory(true).setMandatoryMsg("Last Name is mandatory!").setRowSpan(0).setColSpan(0).build();
		
		fbuilder = new FormFieldBuilder("* Email",etbEmail);
		FormField fetbEmail= fbuilder.setMandatory(true).setMandatoryMsg("Email Format is Wrong").setRowSpan(0).setColSpan(0).build();
		
		
		
		fbuilder = new FormFieldBuilder("* Cell No 1",pnbCellNo1);
		FormField fpnbCellNo1= fbuilder.setMandatory(true).setMandatoryMsg("Cell No. 1 is mandatory!").setRowSpan(0).setColSpan(0).build();
		FormField fsalesGrouping=fbuilder.setlabel("Office Use").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		fbuilder = new FormFieldBuilder("* Branch",olbbBranch);
		FormField folbbBranch= fbuilder.setMandatory(true).setMandatoryMsg("Branch is mandatory!").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Sales Person",olbeSalesPerson);
		FormField folbeSalesPerson= fbuilder.setMandatory(true).setMandatoryMsg("Sales Person is mandatory!").setRowSpan(0).setColSpan(0).build();
		
		
		fbuilder = new FormFieldBuilder();
		FormField fgroupingServiceAddress=fbuilder.setlabel("Address").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
		
		fbuilder = new FormFieldBuilder("",serviceAdressComposite);
		FormField fserviceAddressComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
		fbuilder = new FormFieldBuilder();
		
      
		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		////////////////////////////////Lay Out Making Code///////////////////////////////////////////////////////////////
		FormField[][] formfield = {  {fgroupingCustomerInformation},
				{ftbCustomerId},
				{ftbFirstName,ftbMiddleName,ftbLastName,fetbEmail},
				{fpnbCellNo1},
				{fsalesGrouping},
				{folbbBranch,folbeSalesPerson},
				{fgroupingServiceAddress},
				{fserviceAddressComposite}
				
		};

		this.fields=formfield;
	}

	/**
	 * method template to update the model with token entity name
	 */
	@Override
	public void updateModel(Customer model) 
	{
		
		
		if(tbFirstName.getValue()!=null)
			model.setFirstName(tbFirstName.getValue());
		if(tbMiddleName.getValue()!=null)
			model.setMiddleName(tbMiddleName.getValue());
		if(tbLastName.getValue()!=null)
			model.setLastName(tbLastName.getValue());
		model.setFullname();
		if(etbEmail.getValue()!=null)
			model.setEmail(etbEmail.getValue());
		
		if(pnbCellNo1.getValue()!=null)
			model.setCellNumber1(pnbCellNo1.getValue());
		
		
		if(olbbBranch.getValue()!=null)
			model.setBranch(olbbBranch.getValue());
		if(olbeSalesPerson.getValue()!=null)
			model.setEmployee(olbeSalesPerson.getValue());
		if(serviceAdressComposite.getValue()!=null)
			model.setAdress(serviceAdressComposite.getValue());
		
		
		presenter.setModel(model);
	}

	/**
	 * 
	 * method template to update the view with token entity name
	 */
	@Override
	public void updateView(Customer view) 
	{
		
	   tbCustomerId.setValue(view.getCount());
		
	   
		if(view.getFirstName()!=null)
			tbFirstName.setValue(view.getFirstName());
		if(view.getMiddleName()!=null)
			tbMiddleName.setValue(view.getMiddleName());
		if(view.getLastName()!=null)
			tbLastName.setValue(view.getLastName());
		if(view.getEmail()!=null)
			etbEmail.setValue(view.getEmail());
		
		if(view.getCellNumber1()!=null)
			pnbCellNo1.setValue(view.getCellNumber1());
		
		
		
		if(view.getBranch()!=null)
			olbbBranch.setValue(view.getBranch());
		if(view.getEmployee()!=null)
			olbeSalesPerson.setValue(view.getEmployee());
		if(view.getAdress()!=null)
			serviceAdressComposite.setValue(view.getAdress());
		
		presenter.setModel(view);
	}

	// Hand written code shift in presenter


	/**
	 * Toggles the app header bar menus as per screen state
	 */

	public void toggleAppHeaderBarMenu() {
		if(AppMemory.getAppMemory().currentState==ScreeenState.NEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.equals("Save")||text.equals("Discard")||text.equals("Search"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   
			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.EDIT)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.equals("Save")||text.equals("Discard"))
					menus[k].setVisible(true); 

				else
					menus[k].setVisible(false);  		   
			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.VIEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.equals("Discard")||text.equals("Edit")||text.equals("Search"))
					menus[k].setVisible(true); 

				else
					menus[k].setVisible(false);  		   
			}
		}
		
		
	}

	/**
	 * sets the id textbox with the passed count value. 
	 */
	@Override
	public void setCount(int count)
	{
		tbCustomerId.setValue(count);
		System.out.println("Ajay");
	}


	

	
	public ObjectListBox<Branch> getOlbbBranch() {
		return olbbBranch;
	}

	public void setOlbbBranch(ObjectListBox<Branch> olbbBranch) {
		this.olbbBranch = olbbBranch;
	}

	public EmailTextBox getEtbEmail() {
		return etbEmail;
	}

	public void setEtbEmail(EmailTextBox etbEmail) {
		this.etbEmail = etbEmail;
	}

	
	
	

	public TextBox getTbFirstName() {
		return tbFirstName;
	}

	public void setTbFirstName(TextBox tbFirstName) {
		this.tbFirstName = tbFirstName;
	}

	public TextBox getTbMiddleName() {
		return tbMiddleName;
	}

	public void setTbMiddleName(TextBox tbMiddleName) {
		this.tbMiddleName = tbMiddleName;
	}

	public TextBox getTbLastName() {
		return tbLastName;
	}

	public void setTbLastName(TextBox tbLastName) {
		this.tbLastName = tbLastName;
	}

	

	public PhoneNumberBox getPnbCellNo1() {
		return pnbCellNo1;
	}

	public void setPnbCellNo1(PhoneNumberBox pnbCellNo1) {
		this.pnbCellNo1 = pnbCellNo1;
	}

	

	public ObjectListBox<Employee> getOlbeSalesPerson() {
		return olbeSalesPerson;
	}

	public void setOlbeSalesPerson(ObjectListBox<Employee> olbeSalesPerson) {
		this.olbeSalesPerson = olbeSalesPerson;
	}
	
	
	
	//***********************************Buisness Logic Part****************************************************//
	
   

	public IntegerBox getTbCustomerId() {
		return tbCustomerId;
	}

	public void setTbCustomerId(IntegerBox tbCustomerId) {
		this.tbCustomerId = tbCustomerId;
	}

	

	



	

	@Override
	public void setEnable(boolean status)
	{
		super.setEnable(status);
		this.tbCustomerId.setEnabled(false);
		
	}

  
   
  
   








   
}
